// pages/login/login.js
Page({
  data: {
    disabled: true,
    btnstate: 'default',
    account: '',
    password: '',
  },
  onLoad: function (params) {
    let token = wx.getStorageSync('Authorization');
    console.log(token);
    if (token == null) {
      return;
    }
    console.log(token)
    wx.request({
      url: 'https://www.liahnu.top/apis/user/me',
      method: "GET",
      header: {
        "Authorization": token
      },
      success: function (res) {
        if (res.statusCode != 401) {
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1000
          })
          wx.reLaunch({
            url: '/pages/home/home',
          })
        }
      }
    })


  },
  accountInput: function (e) {
    var content = e.detail.value
    if (content != '') {
      this.setData({
        disabled: false,
        btnstate: 'primary',
        account: content
      })
    } else {
      this.setData({
        disabled: true,
        btnstate: 'default'
      })
    }
  },
  pwdBlur: function (e) {
    var password = e.detail.value
    this.setData({
      password: password
    })
  },
  wclogin: function () {
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.liahnu.top/apis/user/loginForWechat',
            header: {
              'content-type': "application/x-www-form-urlencoded",
            },
            data: {
              code: res.code
            },
            method: "POST",
            success: function (res) {
              console.log(res);
              let data = res.data;
              if (data.success === true) {
                console.log(res);
                // 设置token缓存
                wx.setStorage({
                  key: "Authorization",
                  data: res.data.data
                })
                wx.showToast({
                  title: '登录成功',
                  icon: 'success',
                  duration: 1000
                })
                wx.switchTab({
                  url: '/pages/home/home',
                })
              } else {
                wx.showToast({
                  title: data.errorMsg,
                  icon: "error",
                  duration: 2000
                })
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  login: function () {
    //console.log(this.data.account)
    //console.log(this.data.password)

    getApp().request(
      'POST',
      '/apis/user/login', {
        phone: this.data.account,
        password: this.data.password,
      },
      function (res) {
        console.log(res);
        if (res.data.success === true) {
          // 设置token缓存
          wx.setStorage({
            key: 'Authorization',
            data: res.data.data,
          })
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1000,
          })
          wx.switchTab({
            url: '/pages/home/home',
          })
        } else {
          wx.showToast({
            title: data.errorMsg,
            icon: 'error',
            duration: 2000,
          })
        }
      },
      // function (res) {
      //   console.log(res)
      // }
    )
  },
})